<?php

/**
* Title                     : Sax Filters
* Version                   : 1.0
* Author                    : Luis Argerich (lrargerich@yahoo.com)
* Last modification date    : 05-20-2002
* Description               : A set of classes defining interfaces for generic SAX
*                              parsers as well as SAX filters.
*
* Modified	                : John C.Wildenauer
* Date		                : 20.August.2002
* 
*/
class AbstractSAXParser {

	// ----- Private Properties --------------------------------------------- //

	var $listener;


	// ----- Public Methods ------------------------------------------------- //

	function AbstractSAXParser() {}
	
	function parserSetOption($opt,$val) {}
	
	function setListener($obj) {
		$this->listener = $obj;
	}
	
	function startElementHandler($parser, $name, $attribs) {		
		$this->listener->startElementHandler($name, $attribs);
	}
	
	function endElementHandler($parser, $name) {
		$this->listener->endElementHandler($name);
	}
	
	function characterDataHandler($parser, $data) {
		$this->listener->characterDataHandler($data);
	}
	
	function parse() {}
	
}



class ExpatParser extends AbstractSAXParser {
	
	// ----- Private Properties --------------------------------------------- //
	
	var $parser;
	var $filename;
	var $buffer;
	var $error_string;
	var $line;

	// ----- Public Methods ------------------------------------------------- //

	/** JCW - Mod 23.Aug.2002 
	*/
	function ExpatParser($xmlfile=NULL) {
		$this->filename	= $xmlfile;
		$this->parser		= xml_parser_create();
		$this->buffer		= 4096;
		// This has to be set in the digester->parse() method
		//xml_set_object($this->parser, &$this); 
		xml_set_element_handler($this->parser, "startElementHandler", "endElementHandler");
		xml_set_character_data_handler($this->parser, "characterDataHandler");
	}

	function parserSetOption($opt,$val) {
		return xml_parser_set_option($this->parser, $opt, $val);
	}


	/** JCW - Mod 23.Aug.2002
	*/
	function parse($filename=NULL) {
		
		if($filename != NULL) {
			$this->filename = $filename;
		}
		
		
		// Check if we are processing a file
		if(is_file($this->filename)) {
			if (!($fp = fopen($this->filename, "r"))) {
				return 0;
			}

			while ($data = fread($fp, $this->buffer)) {
				if(!xml_parse($this->parser, $data, feof($fp))) {
					$this->error_string = xml_error_string(xml_get_error_code($this->parser));
					$this->line = xml_get_current_line_number($this->parser);
					die("Error: ".$this->error_string." on line ".$this->line);
				}
			}
			
		// Check if we are processing a string variable
		} elseif(is_string($this->filename)) {

				$data = $this->filename;
				if(!xml_parse($this->parser, $data, True)) {
					$this->error_string = xml_error_string(xml_get_error_code($this->parser));
					$this->line = xml_get_current_line_number($this->parser);
					die("Error: ".$this->error_string." on line ".$this->line);
				}

		} else {
			
			return 0;
		}

		xml_parser_free($this->parser);
		
		return 1;
		
	}
 
}

?>